<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="100px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="轮播图标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="轮播图描述" prop="description">
        <el-input type="textarea"  v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="轮播图地址" prop="url">
        <!-- <el-input v-model="form.url"></el-input> -->
        <file-upolad  @success="uploadSuccess"/>
      </el-form-item>
      <el-form-item label="所属课程" prop="courseId">
        <el-select v-model="form.courseTypeId" clearable filterable @change="chooseCourseType">
          <el-option
            label="-请选择课程分类-"
            value=""
          />
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-select v-model="form.courseId" clearable filterable>
          <el-option
            label="-请选择课程-"
            value=""
          />
          <el-option
            v-for="item in courseOptions"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number :min="0" v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import FileUpolad from '@/components/FileUpload'
import bannerApi from "@/api/banner";
import courseTypeApi from '@/api/courseType'
import courseDetailApi from '@/api/courseDetail'
export default {
  name: "bannerSave",
  components: {
    FileUpolad,
  },
  props: {
    bannerId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },
 
  data() {
    return {
      options: [],
      courseOptions:[],
      form: {
        id:'',
        title:'',
        description:'',
        views:0,
        memberId:'1',
        categoryId:'',
        boutique:0,
        status:1,
        sort:0
      },
      rules: {
        title: [{ required: true, message: "轮播图标题不能为空", trigger: "blur" }],
        description: [{ required: true, message: "轮播图描述不能为空", trigger: "blur" }],
        url: [{ required: true, message: "轮播图地址不能为空", trigger: "blur" }],
        courseId: [{ required: true, message: "课程分类不能为空", trigger: "change" }],
      },
    };
  },
   watch: {
    close(newValue, oldValue) {
      this.form = {
        id:'',
        title:'',
        description:'',
        url:'',
        courseTypeId:'',
        courseId:'',
        sort:1,
      };
    },
    bannerId(newValue, oldValue){
      if (this.bannerId) this.getInfo(this.bannerId);
    }
  },
  created() {
    this.getCourseType()
    if (this.bannerId) this.getInfo(this.bannerId);
  },
  methods: {
    uploadSuccess(url){
      console.log(url);
    },
    // 获取课程分类
    async getCourseType(){
      let res = await courseTypeApi.list({pageNum:1,pageSize:99});
      if(res.success){
        this.options = res.data.records
      }else{
        this.options = []
        return this.$message.error(res.msg)
      }
    },
    chooseCourseType(data){
      this.courseOptions = []
      this.getCourse(data)
    },

     // 获取课程列表
    async getCourse(typeId){
      let res = await courseDetailApi.list({typeId:typeId,pageNum:1,pageSize:99});
      if(res.success){
        this.courseOptions = res.data.records
      }else{
        this.options = []
        return this.$message.error(res.msg)
      }
    },
    // 获取数据
    async getInfo(id) {
      let res = await bannerApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.bannerId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await bannerApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await bannerApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>